//
//  TSCVCForget3ResendVeriEmail.m
//  Tsc_IOS
//
//  Created by Rio on 15-9-10.
//  Copyright (c) 2015年 idream. All rights reserved.
//

#import "TSCVCForget3ResendVeriEmail.h"
#import "TSCSDK.h"
#import "TSCVCForget4ResendVeriEmailSuccessful.h"

@implementation TSCVCForget3ResendVeriEmail {
    NSString * _iEmail;
}

#pragma mark 校验并获取输入

- (void) getVCInput:(NSNotificationCenter *)notification {
    [super getVCInput:notification];
    
    _iEmail = [_inputDic objectForKey:AEKI_EMAIL] ;
    
    NSAssert([TSCMyUtil isStringEmpty:_iEmail] == NO, @"%s: 输入参数必须包括：%@！", __FUNCTION__, AEKI_EMAIL);
}

+ (id) vcForget3ResendVeriEmailWithStoryboard {
    UIStoryboard * sb = [UIStoryboard storyboardWithName:UI_STORYBOARD_NAME_MAIN bundle:[NSBundle mainBundle]];
    return [sb instantiateViewControllerWithIdentifier:UI_VC_NAME_FORGET_3_RESEND_VERI_EMAIL];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 设置自定义导航栏
    _nbc = [[TSCMyNaviBarCreator alloc]initAndCreateWithViewController:self hasGoBackButton:YES goBackVC:nil hasAppLogo:YES titleResourceKey:@"title_forget_check_email" iconResourceNameArray:nil iconClickedListenerArray:nil];
    
    // 设置界面显示
    _lbInfo.text = [NSString stringWithFormat:@"%@%@%@", NSLocalizedString(@"content_check_email_1", nil), _iEmail, NSLocalizedString(@"content_check_email_2", nil)];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

#pragma mark 私有方法



- (void) sendVerifyEmail_S1:(NSString *)emailAddress {
    // 拼装请求
    NSMutableDictionary * reqDic = [[NSMutableDictionary alloc] init];
    [reqDic setObject:emailAddress forKey:@"email"];
    
    // 发送请求
    [TSCMyHttpUtil postWithUrl:URL_FORGET_PASSWD_SEND_VERIFY_EMAIL_EMAIL dictionary:reqDic completionHandler:^(ServerRespType serverRespType, NSString * respString, NSDictionary *respDictionary, NSInteger code, NSString *msg, NSDictionary *contentDictionary) {
        // 服务器返回错误处理
        switch (serverRespType) {
            case ServerRespTypeSuccess:
                break;
            case ServerRespTypeErrNil: {
                [self dissmisDialog];
                [TSCMyUIUtil popErrWithResourceKey:@"err_web_server_unavailable"];
                return;
            }
            case ServerRespTypeErrJson: {
                [self dissmisDialog];
                
                // 2015-01-19 Rio
                // 注：如果发送邮件的邮箱地址不存在，phpmailer.php会向控制台输出：SMTP Error: The
                // following recipients failed:
                // dao2@126.com，从而导致APP解析json字符串失败
                // 解决办法一：将phpmailer.php的输出语句注释掉（下面两句）
                // 解决办法二：在APP端处理，当发现返回中包括“SMTP Error: The following
                // recipients failed:”的字样时，就认为邮箱地址不存在
                // 暂时采用办法二
                if ([respString hasPrefix:ERR_PHP_MAILER_NONE_EXIST_EMAIL_ADDRESS]) {
                    [TSCMyUIUtil popErrWithResourceKey:@"err_send_email_failed"];
                } else {
                    [TSCMyUIUtil popErrWithResourceKey:@"err_web_server_unavailable"];
                }
                return;
            }
            case ServerRespTypeErrCode:{
                [self dissmisDialog];
                [TSCMyUIUtil popErrWithString:msg];
                return;
            }
            default: {
                [self dissmisDialog];
                [TSCMyUIUtil popErrWithResourceKey:@"err_web_server_unavailable"];
                return;
            }
                
        }
        
        // 无返回内容，无需解析
        
        // 继续后续逻辑...
        [self sendVerifyEmail_S2];
    }];
    
}

- (void) sendVerifyEmail_S2 {
    [self dissmisDialog];
    
    TSCVCForget4ResendVeriEmailSuccessful *vc = [TSCVCForget4ResendVeriEmailSuccessful vcForget4ResendVeriEmailSuccessfulWithStoryboard];
    [[self navigationController] pushViewController:vc animated:YES];
    
    // VC间传递参数
    NSDictionary * inputDic = [NSDictionary dictionaryWithObject:[TSCMyUtil trim:_iEmail] forKey:AEKI_EMAIL];
    [[NSNotificationCenter defaultCenter] addObserver:vc selector:@selector(SELECTOR_GET_VC_INPUT) name:UI_VC_NAME_FORGET_4_RESEND_VERI_EMAIL_SUCCESSFUL object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:UI_VC_NAME_FORGET_4_RESEND_VERI_EMAIL_SUCCESSFUL object:inputDic];
}



#pragma mark delegate

- (IBAction)btnResendVeriEmailClicked:(id)sender {
    // 提示用户正在发送
    [self showDialogWithResourceKey:@"content_sending" isProcessing:YES];
    
    [self sendVerifyEmail_S1:_iEmail];
}


@end
